﻿using IdentityServer4.Models;
using IdentityServer4.Test;
using System.Collections.Generic;
using System.Linq;

namespace IdentityServer
{
    public class ApiConfig
    {
        /// <summary>
        /// 定义ApiResource   这里的资源（Resources）指的就是我们的API
        /// </summary>
        /// <returns>ApiResource枚举</returns>
        public static IEnumerable<ApiResource> GetApiResources()
        {
            return UserApi.ApiNames.Select(a => new ApiResource(a, a) { Scopes = { a } });
        }

        /// <summary>
        /// 定义受信任的客户端 Client
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Client> GetClients()
        {
            return new[]
            {
                new Client
                {
                    ClientId = UserApi.ClientId,//客户端的标识，要是惟一的
                    ClientSecrets = new [] { new Secret(UserApi.Secret.Sha256()) },//客户端密码，进行了加密
                    AllowedGrantTypes =  GrantTypes.ResourceOwnerPassword,//授权方式
                    AllowedScopes= UserApi.ApiNames,//定义这个客户端可以访问的APi资源数组，上面只有一个api
                    AccessTokenLifetime =   ExpireIn
                }
            };
        }

        // <summary>
        /// 测试的账号和密码
        /// </summary>
        /// <returns></returns>
        public static List<TestUser> GetTestUsers()
        {
            return new List<TestUser>
            {
                new TestUser()
                {
                    SubjectId = "1",
                    Username = "test",
                    Password = "123456"
                }
            };
        }

        /// <summary>
        /// 过期秒数
        /// </summary>
        public const int ExpireIn = 36000;

        /// <summary>
        /// 用户Api相关
        /// </summary>
        public static class UserApi
        {
            public static string[] ApiNames = { "base_api", "new_api", "other_api" };

            public static string ClientId = "user_clientid";

            public static string Secret = "user_secret";
        }


        public static IEnumerable<ApiScope> ApiScopes => UserApi.ApiNames.Select(a => new ApiScope(a));
    }
}
